package com.sunrun.sunrunframwork.uibase;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunrun.sunrunframwork.BaseApplication;
import com.sunrun.sunrunframwork.http.BaseBean;
import com.sunrun.sunrunframwork.http.NetServer;
import com.sunrun.sunrunframwork.http.cache.NetSession;
import com.sunrun.sunrunframwork.http.intf.UIUpdateHandler;
import com.sunrun.sunrunframwork.utils.log.Logger;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * @fragment 基类, 用于初始化一些公共的东西, 使得Fragment更好使用
 */
public abstract class BaseFragment extends Fragment  implements UIUpdateHandler {
    protected FragmentActivity _CONTEXT;
    Unbinder unbinder;
    NetServer netServer;
    public NetServer getNetRequest() {
        if(netServer==null){
            netServer=new NetServer(getActivity(),this);
        }
        return netServer;
    }
    protected abstract int getLayoutId();
    @Override
    public abstract void onViewCreated(View view, @Nullable Bundle savedInstanceState);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        _CONTEXT = getActivity();
        BaseApplication.getInstance().addFragment(this);
        super.onCreate(savedInstanceState);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        unbinder=ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    public  void onVisible() {

    }

    public  void onInVisible() {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            onInVisible();
        }else{
            onVisible();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            onVisible();
        }else{
            onInVisible();
        }
    }

    @Override
    public void nofityUpdate(int requestCode, BaseBean bean) {

    }

    @Override
    public void nofityUpdate(int requestCode, float progress) {

    }

    @Override
    public void dealData(int requestCode, BaseBean bean) {

    }

    @Override
    public void loadFaild(int requestCode, BaseBean bean) {

    }

    @Override
    public void emptyData(int requestCode, BaseBean bean) {

    }

    @Override
    public void requestFinish() {

    }

    @Override
    public void requestCancel() {

    }

    @Override
    public void requestStart() {

    }

    public NetSession getSession() {
        return NetSession.instance(_CONTEXT);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        BaseApplication.getInstance().removeFragment(this);
        Logger.D("视图销毁 " + this);
        _CONTEXT = null;
    }


    public void finish() {
        if (_CONTEXT != null)
            _CONTEXT.finish();
    }

}
